package com.itheima.controller;

import com.itheima.pojo.PublishWork;
import com.itheima.pojo.ShowWorks;
import com.itheima.pojo.User;
import com.itheima.pojo.vo.UserOv;
import com.itheima.service.WorkService;
import com.itheima.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 28066
 * @version 1.0
 * @description: TODO
 * @date 2022/11/13 19:39
 */
@RestController
@RequestMapping("/v1/work")
public class WorkController {
    @Autowired
    private WorkService workService;

    @PostMapping("/publishWork")
    public Result publishWork(@RequestBody PublishWork publishWork, HttpSession session){
        User user = (User) session.getAttribute("User");
        Integer id = user.getId();

        publishWork.setUserId(id);
        int i=workService.addNewWork(publishWork);
        if (id == 1 && i>0){
            return new Result(0,"操作成功","发布作业成功");
        }
        return new Result(1,"操作失败");
    }

    @PostMapping("/showWorks")
    public Result showWorks(@RequestBody ShowWorks showWorks){
        List<UserOv> list=workService.showWorks(showWorks);
        list.forEach(System.out::println);
        if(list!= null){
            return new Result(0,"操作成功",list);
        }
        return new Result(1,"操作失败");
    }
}
